/*	BuildPhysicsLab class definition.
 *
*/

#include "BuildingPlacer.h"
#include "BuildPhysicsLab.h"

void BuildPhysicsLab::Enter()
{
	DEBUG_PRINT("BuildPhysicsLab(%p) %s entering\n", this, GetName().c_str());

}

void BuildPhysicsLab::Execute()
{
	DEBUG_PRINT("BuildPhysicsLab(%p) %s executing\n", this, GetName().c_str());
	std::list<Base*>* bases = BB->GetBases();
	std::list<BWAPI::Unit*>* facilities;
	BWAPI::Unit* sci = NULL;
	for(std::list<Base*>::iterator base = bases->begin(); base != bases->end() && !sci; base++)
	{
		facilities = (*base)->GetBuildingsByType(BWAPI::UnitTypes::Terran_Science_Facility);
		if(facilities)
		{
			for(std::list<BWAPI::Unit*>::iterator it = facilities->begin(); it != facilities->end(); it++)
			{
				if(!(*it)->getAddon())
				{
					sci = (*it);
					break;
				}
			}
		}
	}
	if(sci)
	{
		sci->buildAddon(BWAPI::UnitTypes::Terran_Physics_Lab);
		BB->SpendResources(BWAPI::UnitTypes::Terran_Physics_Lab);
	}
	control->FinishWithSuccess();
}

void BuildPhysicsLab::Exit()
{
	DEBUG_PRINT("BuildPhysicsLab(%p) %s exiting\n", this, GetName().c_str());
	
}

bool BuildPhysicsLab::CheckConditions()
{
	DEBUG_PRINT("BuildPhysicsLab(%p) %s checking conditions\n", this, GetName().c_str());
	
	std::list<BWAPI::Unit*>* sci = BB->GetBuildingsByTypeGeneral(BWAPI::UnitTypes::Terran_Science_Facility);
	std::list<BWAPI::Unit*>* lab = BB->GetBuildingsByTypeGeneral(BWAPI::UnitTypes::Terran_Physics_Lab);
	if(sci)
	{
		if(!lab)
		{
			if(BB->isBuildBuilding(BWAPI::UnitTypes::Terran_Physics_Lab) )
			{
				DEBUG_PRINT("BuildPhysicsLab(%p) %s checked conditions and has %d minerals to spend\n", this, GetName().c_str(), BB->GetMineralsToSpend());
				return true;
			}
		}
	}
	

	return false;
}